package com.dyh.quickdemozwei.ptzj;

import com.alibaba.fastjson.JSONObject;

import java.util.Optional;


public interface FiledZjListener<V> {
    String path();

    Class<V> cls();

    default V getVal(JSONObject target){
        String[] arr = path().split("/");

        JSONObject node = target;

        for (int i = 0; i < arr.length-1; i++) {
            node = node.getJSONObject(arr[i]);
            if(node==null)
                break;
        }

        return Optional.ofNullable(node).map(e->e.get(arr[arr.length-1]))
                .map(e->cls().cast(e)).orElse(null);


    }

    boolean atRisk(V val,ZJItem zjItem);


     FiledZjListener<String> titileListener = new FiledZjListener<String>() {
        @Override
        public String path() {
            return "prod/title";
        }

        @Override
        public Class<String> cls() {
            return String.class;
        }

        @Override
        public boolean atRisk(String val, ZJItem zjItem) {

            //zjItem 中拿 违禁词 、白名单
            return val.contains("违禁词");
        }
    };

    public static void main(String[] args) {
        JSONObject jo = JSONObject.parseObject("{\"prod\":{\"title\":\"含有违禁词的标题\"}}");
        ZJItem zjItem = new ZJItem();
        boolean atRisk =  titileListener.atRisk(titileListener.getVal(jo),zjItem)  ;

        System.out.println();

    }



}
